<template>
  <div class="warpper-content">
    <div class="common-cont">
      <el-row :gutter="12" v-if="ServerOS">
        <el-col :span="8">
          <el-card shadow="hover" class="server-card">
            <template #header>
              <div class="clearfix">
                <span>CPU使用率</span>
                <el-link type="primary" style="float: right; padding: 3px 0" :underline="false" @click="getInfo">刷新
                </el-link>
              </div>
            </template>
            <el-row>
              <el-col :span="24" style="text-align:center">
                <el-progress type="dashboard" :percentage="ServerOS.cpu.used" :color="colors"></el-progress>
              </el-col>
              <el-col :span="24">
                <div class="el-table el-table--enable-row-hover el-table--medium">
                  <table cellspacing="0" style="width: 100%;">
                    <tbody>
                      <tr>
                        <td>
                          <div class="cell">CPU主频</div>
                        </td>
                        <td>
                          <div class="cell">{{ ServerOS.cpu.cpuCard.processorIdentifier.name.split('@')[1] }}</div>
                        </td>
                      </tr>
                      <tr>
                        <td>
                          <div class="cell">核心数</div>
                        </td>
                        <td>
                          <div class="cell">{{ ServerOS.cpu.num }}</div>
                        </td>
                      </tr>
                    </tbody>
                  </table>
                </div>
              </el-col>
            </el-row>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card shadow="hover" class="server-card">
            <template #header>
              <div class="clearfix">
                <span>内存使用率</span>
                <el-link type="primary" style="float: right; padding: 3px 0" :underline="false" @click="getInfo">刷新
                </el-link>
              </div>
            </template>
            <el-row>
              <el-col :span="24" style="text-align:center">
                <el-progress type="dashboard" :percentage="ServerOS.mem.used" :color="colors"></el-progress>
              </el-col>
              <el-col :span="24">
                <div class="el-table el-table--enable-row-hover el-table--medium">
                  <table cellspacing="0" style="width: 100%;">
                    <tbody>
                      <tr>
                        <td>
                          <div class="cell">总内存</div>
                        </td>
                        <td>
                          <div class="cell">{{ ServerOS.mem.total }}G</div>
                        </td>
                      </tr>
                      <tr>
                        <td>
                          <div class="cell">已用内存</div>
                        </td>
                        <td>
                          <div class="cell">{{ ServerOS.mem.usedMem }}G</div>
                        </td>
                      </tr>
                    </tbody>
                  </table>
                </div>
              </el-col>
            </el-row>

          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card shadow="hover" class="server-card">
            <template #header>
              <div class="clearfix">
                <span>JVM使用率</span>
                <el-link type="primary" style="float: right; padding: 3px 0" :underline="false" @click="getInfo">刷新
                </el-link>
              </div>
            </template>
            <el-row>
              <el-col :span="24" style="text-align:center">
                <el-progress type="dashboard" :percentage="ServerOS.jvm.used" :color="colors"></el-progress>
              </el-col>
              <el-col :span="24">
                <div class="el-table el-table--enable-row-hover el-table--medium">
                  <table cellspacing="0" style="width: 100%;">
                    <tbody>
                      <tr>
                        <td>
                          <div class="cell">JVM大小</div>
                        </td>
                        <td>
                          <div class="cell">{{ ServerOS.jvm.total }}M</div>
                        </td>
                      </tr>
                      <tr>
                        <td>
                          <div class="cell">已用JVM</div>
                        </td>
                        <td>
                          <div class="cell">{{ ServerOS.jvm.usedMem }}M</div>
                        </td>
                      </tr>
                    </tbody>
                  </table>
                </div>
              </el-col>
            </el-row>
          </el-card>
        </el-col>
        <el-col :span="24">
          <el-card shadow="hover" class="server-card">
            <template #header>
              <div class="clearfix">
                <span>JAVA虚拟机信息</span>
              </div>
            </template>
            <div class="el-table el-table--enable-row-hover el-table--medium">
              <table v-if="ServerOS" cellspacing="0" style="width: 100%;">
                <tbody>
                  <tr>
                    <td>
                      <div class="cell">JAVA名称</div>
                    </td>
                    <td>
                      <div class="cell">{{ ServerOS.sysInfo['java.vm.name'] }}</div>
                    </td>
                  </tr>
                  <tr>
                    <td>
                      <div class="cell">JAVA版本</div>
                    </td>
                    <td>
                      <div class="cell">{{ ServerOS.sysInfo['java.version'] }}</div>
                    </td>
                  </tr>
                  <tr>
                    <td>
                      <div class="cell">Java的安装路径 </div>
                    </td>
                    <td>
                      <div class="cell">{{ ServerOS.sysInfo['java.home'] }}</div>
                    </td>
                  </tr>
                  <tr>
                    <td>
                      <div class="cell">Java供应商</div>
                    </td>
                    <td>
                      <div class="cell">{{ ServerOS.sysInfo['java.vendor'] }}</div>
                    </td>
                  </tr>
                  <tr>
                    <td>
                      <div class="cell">Jvm最大可用内存</div>
                    </td>
                    <td>
                      <div class="cell">{{ ServerOS.jvm.maxTotal }}M</div>
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>
          </el-card>
        </el-col>
        <el-col :span="24">
          <el-card shadow="hover" class="server-card">
            <template #header>
              <div class="clearfix">
                <span>磁盘信息</span>
              </div>
            </template>
            <div class="el-table el-table--enable-row-hover el-table--medium">
              <table v-if="ServerOS" cellspacing="0" style="width: 100%;">
                <tbody>
                  <tr>
                    <td>
                      <div class="cell">磁盘名称</div>
                    </td>
                    <td>
                      <div class="cell">大小</div>
                    </td>
                    <td>
                      <div class="cell">已用</div>
                    </td>
                    <td>
                      <div class="cell">可用</div>
                    </td>
                    <td>
                      <div class="cell">使用率</div>
                    </td>
                  </tr>
                  <tr v-for="(disk, index) in ServerOS.file" :key="index">
                    <td>
                      <div class="cell">{{ disk.name }}</div>
                    </td>
                    <td>
                      <div class="cell">{{ disk.total }}</div>
                    </td>
                    <td>
                      <div class="cell">{{ disk.used }}</div>
                    </td>
                    <td>
                      <div class="cell">{{ disk.free }}</div>
                    </td>
                    <td>
                      <div class="cell">
                        <el-progress :percentage="disk.rate"></el-progress>
                      </div>
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>
          </el-card>
        </el-col>
        <el-col :span="24">
          <el-card shadow="hover" class="server-card">
            <template #header>
              <div class="clearfix">
                <span>服务器信息</span>
              </div>
            </template>
            <div class="el-table el-table--enable-row-hover el-table--medium">
              <table v-if="ServerOS" cellspacing="0" style="width: 100%;">
                <tbody>
                  <tr>
                    <td>
                      <div class="cell">服务器名称</div>
                    </td>
                    <td>
                      <div class="cell">{{ ServerOS.hostName }}</div>
                    </td>
                  </tr>
                  <tr>
                    <td>
                      <div class="cell">操作系统</div>
                    </td>
                    <td>
                      <div class="cell">{{ ServerOS.sysInfo['os.name'] }}{{ ServerOS.sysInfo['os.version'] }}</div>
                    </td>
                  </tr>
                  <tr>
                    <td>
                      <div class="cell">服务器IP</div>
                    </td>
                    <td>
                      <div class="cell">{{ ServerOS.ip }}</div>
                    </td>
                  </tr>
                  <tr>
                    <td>
                      <div class="cell">系统架构</div>
                    </td>
                    <td>
                      <div class="cell">{{ ServerOS.sysInfo['os.arch'] }}</div>
                    </td>
                  </tr>
                  <tr>
                    <td>
                      <div class="cell">CPU</div>
                    </td>
                    <td>
                      <div class="cell">{{ ServerOS.cpu.cpuCard.processorIdentifier.name }}</div>
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script lang="ts">
export default { name: 'MainDashBoard' }
</script>

<script setup lang="ts">
import { reactive } from 'vue'
import { getSystemInfo } from 'src/api/home'
import Server from './constants'

const ServerOS = reactive(Server)

const getInfo = () => {
  getSystemInfo().then(({ data }) => {
    Object.assign(ServerOS, data)
  })
}

getInfo()

const colors = [
  { color: '#0cc26c', percentage: 20 },
  { color: '#0cc26c', percentage: 40 },
  { color: '#eb6607', percentage: 60 },
  { color: '#f5212d', percentage: 80 },
  { color: 'red', percentage: 100 }
]
</script>

<style>
.server-card {
  margin-bottom: 18px;
  /* padding-bottom: 20px; */
}
</style>
